package com.pmsystem.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pmsystem.pojo.Building;
import com.pmsystem.pojo.BuildingShow;
import com.pmsystem.service.BuildingService;


/**
 * 
* <p>Title: BuildingController</p>  
* <p>Description:处理大楼信息的控制器 </p>  
* @author 树昌  
* @date 2019年1月21日
 */
@Controller
@RequestMapping("Information/")
public class BuildingController {

	@Autowired
	BuildingService buildingService;
	
	/**
	 * 
	 * <p>Title: addBuilding</p>  
	 * <p>Description:添加大楼信息 </p>  
	 * @author 树昌
	 * @param building
	 * @return 
	 */
	@ResponseBody
	@RequestMapping(value="addbuilding" ,method=RequestMethod.POST)
	public Map<String, Object> addBuilding(Building building) {
		
		Map<String, Object> map = new HashMap<>();
		boolean row = buildingService.addBuilding(building);
		String msg = null;
		if(row) {
			msg = "success";
		}else {
			msg = "error";
		}
		map.put("msg", msg);
		return map;
	}
	
	
	/**
	 * 
	 * <p>Title: showBuilding</p>  
	 * <p>Description:显示所有大楼信息的控制器 </p>  
	 * @author 树昌
	 * @return 不做跳转
	 */
	@RequestMapping(value="showbuilding",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> showBuilding(String keyword,Integer VID) {
		
		Map<String, Object> map = new HashMap<>();
		List<BuildingShow> listbuilding = buildingService.showBuilding(keyword,null,null,VID);
		
		map.put("listbuilding", listbuilding);
		int size = listbuilding.size();
		map.put("size", size);
		return map;
	}
	
	
	/**
	 * 
	 * <p>Title: showBuildingByKey</p>  
	 * <p>Description:根据ID显示一条大楼信息 </p>  
	 * @author 树昌
	 * @param id
	 * @return
	 */
	@RequestMapping(value="showBuildingByKey",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> showBuildingByKey(int id) {
		
		Map<String, Object> map = new HashMap<>();
		Building building = buildingService.showOneBuilding(id);
		map.put("building", building);
		return map;
	}
	
	
	/**
	 * 
	 * <p>Title: deleteBuilding</p>  
	 * <p>Description:删除一条大楼的记录的控制器 </p>  
	 * @author 树昌
	 * @param building
	 * 不做跳转
	 */
	@RequestMapping(value="deletebuilding" ,method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> deleteBuilding(int id) {
		
		Map<String, Object> map = new HashMap<>();
		String msg=null;
		Building building = new Building();
		building.setBuildingId(id);
		boolean row = buildingService.deleteBuilding(building);
		if(row) {
			msg = "success";
		}else {
			msg = "error";
		}
		map.put("msg", msg);
		return map;
	}
	
	
	/**
	 * 
	 * <p>Title: deleteAllBuildings</p>  
	 * <p>Description:批量删除大楼的记录 </p>  
	 * @author 树昌
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="deleteAllBuildings",method=RequestMethod.POST)
	public Map<String, Object> deleteAllBuildings(int [] id) {
		Map<String, Object> map = new HashMap<>();
		String msg=null;
		List<Integer> listID = new ArrayList<>();
		for (int i : id) {
			listID.add(i);
		}
		
		boolean row = buildingService.deleteAllBuilding(listID);
		
		if(row) {
			msg = "success";
		}else {
			msg = "error";
		}
		map.put("msg", msg);
		return map;
	}
	
	
	/**
	 * 
	 * <p>Title: updateBuilding</p>  
	 * <p>Description:修改一条大楼记录的控制器 </p>  
	 * @author 树昌
	 * @param building
	 * @return 跳转到显示所有大楼信息的页面
	 */
	@ResponseBody
	@RequestMapping(value="updatebuilding" ,method=RequestMethod.POST)
	public Map<String, Object> updateBuilding(Building building) {
		
		Map<String, Object> map = new HashMap<>();
		String msg=null;
		boolean row = buildingService.updateBuilding(building);
		if(row) {
			msg="success";
		}else {
			msg="error";
		}
		
		map.put("msg", msg);
		return map;
	}
	
	
}
